package com.gxuwz.Market.business.action.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceClass;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.service.IServiceAcademyService;
import com.gxuwz.Market.business.service.IServiceClassService;
import com.gxuwz.Market.business.service.IServiceMajorService;
import com.gxuwz.Market.business.service.ISysUserRoleService;
import com.gxuwz.Market.business.service.ISysUserService;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ServiceClassAction extends BaseAction implements Preparable, ModelDriven<ServiceClass> {

	protected static final String LIST_JSP = "/WEB-INF/page/web/ServiceClass_list.jsp";  
	protected static final String ADD_JSP = "/WEB-INF/page/web/ServiceClass_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/web/ServiceClass_edit.jsp";
	
	@Autowired
	private IServiceClassService serviceClassService;
	@Autowired
	private IServiceMajorService serviceMajorService;
	private List<ServiceAcademy>academyList;//学院列表
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	public List<ServiceAcademy> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<ServiceAcademy> academyList) {
		this.academyList = academyList;
	}

	private ServiceClass serviceClass;
	private Result<ServiceClass> pageResult;
	private List<ServiceMajor> majorList;
	private Map<String,String> classMap = new HashMap<String, String>();
	
	
	
	@Override
	public void prepare() throws Exception {
		if(null == serviceClass){
			serviceClass = new ServiceClass();
		}
	}

	@Override
	public ServiceClass getModel() {
		return serviceClass;
	}
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	
	/**
	 * 打开列表页面
	 * @return
	 */
	public String openList(){
		pageResult = serviceClassService.find(serviceClass, getPage(), getRow());
		
		try{
			for(ServiceClass cla:pageResult.getData()){
				ServiceMajor major = serviceMajorService.findByProperty("majorId", cla.getMajorId()).get(0);
				classMap.put(cla.getClassId(), major.getMajorName());
			}
		}catch(Exception e){
			System.out.println(e);
		}
		majorList =serviceMajorService.findAll();
		academyList= sysUserService.findAllAcademy();
		
		setForwardView(LIST_JSP);
		return SUCCESS;
		
	}


	/**
	 * 删除
	 * @return
	 */
	public String delete() {
		serviceClassService.delete(serviceClass.getId());
		serviceClass.setId(null);
		return openList();
	}
	
	
	
	/**
	 * 打开修改页面
	 * @return
	 */
	public String openEdit(){
		serviceClass =serviceClassService.findById(serviceClass.getId());
        majorList =serviceMajorService.findAll();
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}

	public String openAdd() {
		majorList =serviceMajorService.findAll();
		
		
		
		setForwardView(ADD_JSP);
		
		return SUCCESS;
	}
	
	public String add() {
		serviceClassService.add(serviceClass);
		serviceClass = new ServiceClass();
		return openList();
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String edit(){
		serviceClassService.update(serviceClass);
		serviceClass =new ServiceClass();
		return openList();
	}
	
	public ServiceClass getServiceClass() {
		return serviceClass;
	}

	public void setServiceClass(ServiceClass serviceClass) {
		this.serviceClass = serviceClass;
	}

	public Result<ServiceClass> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<ServiceClass> pageResult) {
		this.pageResult = pageResult;
	}

	

	public List<ServiceMajor> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<ServiceMajor> majorList) {
		this.majorList = majorList;
	}

	public Map<String, String> getClassMap() {
		return classMap;
	}

	public void setClassMap(Map<String, String> classMap) {
		this.classMap = classMap;
	}

	







	
	
	
}
